<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Convertion_history_entity{
	var $id = 0;
	var $user_id = 0;
	var $match_id = 0;
	var $tournament_id = 0;
	var $point = 0;
	var $coin = 0;
	var $time = 0;
	var $status = "";
	var $convert_rate = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_CONVERSION)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->user_id = $result[0]->user_id;
			$this->match_id = $result[0]->match_id;
			$this->point = $result[0]->point;
			$this->coin = $result[0]->coin;
			$this->time = $result[0]->time;
			$this->status = $result[0]->status;
			$this->convert_rate = $result[0]->convert_rate;
			$this->tournament_id = $result[0]->tournament_id;
			
			return $this;
		}
		return false;
	}
	
	function setUserId($input){ $this->user_id=$input; }
	function setMatchId($input){ $this->match_id=$input;}
	function setPoint($input){ $this->point=$input; }
	function setCoin($input){ $this->coin=$input;}
	function setTime($input){ $this->time=$input; }
	function setStatus($input){ $this->status=$input;}
	function setConvertRate($input){ $this->convert_rate=$input;}
	function setTournamentId($input){ $this->tournament_id=$input;}
	
	function getId(){ return $this->id; }
	function getUserId(){ return $this->user_id; }
	function getMatchId(){ return $this->match_id;}
	function getPoint(){ return $this->point;}
	function getCoin(){ return $this->coin;}
	function getTime(){ return $this->time;}
	function getStatus(){ return $this->status;}
	function getConvertRate(){ return $this->convert_rate;}
	function getTournamentId(){ return $this->tournament_id;}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('user_id', $this->user_id);
		$ci->db->set('match_id', $this->match_id);
		$ci->db->set('point', $this->point);
		$ci->db->set('coin', $this->coin);
		$ci->db->set('time', $this->time);
		$ci->db->set('status', $this->status);
		$ci->db->set('convert_rate', $this->convert_rate);
		$ci->db->set('tournament_id', $this->tournament_id);
		
		$ci->db->where('id', $this->id)->update(TBL_CONVERSION);
	}
	
	function createNewConvertionHistoryEntity(){
		$ci=& get_instance();
		
		$ci->db->set('status','');
		$ci->db->insert(TBL_CONVERSION);
		
		return $ci->db->insert_id();
	}
	
	function getConvertionHistoryItemsByUserId($user_id){
		$ci=& get_instance();
		return $ci->db->where('user_id',$user_id)->order_by('time','desc')->get(TBL_CONVERSION)->result();
	}
	
	function getConvertionHistoryMatch($user_id, $tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array( 'user_id'=>$user_id, 'tournament_id'=>$tournament_id ) )
		->order_by('time','desc')->get(TBL_CONVERSION)->result();
	}
	
	function getConvertionHistoryItems($user_id, $tournament_id, $match_id){
		$ci=& get_instance();
		return $ci->db->where( array( 'user_id'=>$user_id, 'tournament_id'=>$tournament_id, 'match_id'=>$match_id ) )
		->order_by('time','desc')->get(TBL_CONVERSION)->result();
	}
	
	function getConvertionHistory($user_id){
		$arrayReturn = array();
		$tmpTournamentArray = array();
		$tmpMatchArray = array();
		
		foreach($this->getConvertionHistoryItemsByUserId($user_id) as $item){
			$tmpTournamentArray[] = $item->tournament_id;
		}
		$tmpTournamentArray = array_unique($tmpTournamentArray);
		foreach($tmpTournamentArray as $t_id){
			$arrayReturn[$t_id] = array();
			foreach($this->getConvertionHistoryMatch($user_id,$t_id) as $item){
				$tmpMatchArray[] = $item->match_id;
			}
			$tmpMatchArray = array_unique($tmpMatchArray);
			foreach($tmpMatchArray as $m_id){
				$arrayReturn[$t_id][$m_id] = array();
				foreach($this->getConvertionHistoryItems($user_id, $t_id, $m_id) as $item){
					$arrayReturn[$t_id][$m_id][] = $item->id;
				}
			}
		}
		return $arrayReturn;
	}
	
}
